from operator import itemgetter

from langchain_community.llms import Tongyi
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

llm = Tongyi()

planner = (
        ChatPromptTemplate.from_template(
            "生成一个关于{input}的论点，请尽量简洁一些"
        )
        | llm
        | StrOutputParser()
        | {"base_response": RunnablePassthrough()}
)

arguments_for_planner = (
        ChatPromptTemplate.from_template(
            "列出以下内容的优点：{base_response},请尽量简洁一些")
        | llm
        | StrOutputParser()
)

arguments_for_again = (
        ChatPromptTemplate.from_template(
            "列出以下内容的缺点：{base_response},请尽量简洁一些"
        ) | llm | StrOutputParser()
)
final_ss =(ChatPromptTemplate.from_messages(
    [
        ("ai","{origin_respond}"),# AI原始论点
        ("human","积极：\n{result_1} \n \n 消极：\n{result_2}"),#用户的观点
        ("system","根据评论生成最终的回复,只需要回复总结即可，不需要分析"),
    ]
) | llm | StrOutputParser())

chat_prompt = (planner | {
    "result_1":arguments_for_planner,
    "result_2":arguments_for_again,
    "origin_respond":itemgetter("base_response")
}| final_ss)

print(chat_prompt.invoke({'input': "是否有外星人"}))
